using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using System;
using System.IO.IsolatedStorage;
namespace MicroMsg.Scene.Image
{
	public class DownloadImgContext : IContextBase
	{
		private const string TAG = "DownloadImgContext";
		public const int STATUS_INIT = 0;
		public const int STATUS_READY = 1;
		public const int STATUS_LOADING = 2;
		public const int STATUS_ERROR = 4;
		public const int STATUS_COMPLETE = 5;
		public int mStatus;
		public string talkerName = "";
		public IsolatedStorageFileStream fileStream;
		public MsgTrans mImgInfo;
		public NetSceneDownloadImg senceHandle;
		public bool needToHandle()
		{
			return this.mStatus == 0;
		}
		public bool isRunning()
		{
			if (this.mStatus != 1)
			{
				if (this.mStatus != 2)
				{
					return false;
				}
			}
			return true;
		}
		public bool needToClean()
		{
			return this.mStatus == 5 || this.mStatus == 4;
		}
		public IsolatedStorageFileStream getImgFileStream()
		{
			if (this.mImgInfo == null)
			{
				return null;
			}
			IsolatedStorageFileStream result;
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					if (!userStoreForApplication.DirectoryExists(StorageIO.getImagePath()))
					{
						userStoreForApplication.CreateDirectory(StorageIO.getImagePath());
					}
					if (this.mImgInfo.strImagePath != null && userStoreForApplication.FileExists(this.mImgInfo.strImagePath) && this.mImgInfo.nTotalDataLen > 0 && this.mImgInfo.nTransDataLen < this.mImgInfo.nTotalDataLen)
					{
						IsolatedStorageFileStream isolatedStorageFileStream = userStoreForApplication.OpenFile(this.mImgInfo.strImagePath, 3, 3);
						if (isolatedStorageFileStream.get_Length() == (long)this.mImgInfo.nTransDataLen)
						{
							result = isolatedStorageFileStream;
							return result;
						}
						isolatedStorageFileStream.Close();
					}
					string hashString = MD5Core.GetHashString(this.mImgInfo.strThumbnail + Util.getNowMilliseconds());
					string text = StorageIO.getImagePath() + "/" + hashString + "_recv.jpg";
					if (userStoreForApplication.FileExists(text))
					{
						userStoreForApplication.DeleteFile(text);
					}
					IsolatedStorageFileStream isolatedStorageFileStream2 = userStoreForApplication.CreateFile(text);
					if (this.mImgInfo != null)
					{
						this.mImgInfo.strImagePath = text;
						this.mImgInfo.nTransDataLen = 0;
					}
					result = isolatedStorageFileStream2;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("DownloadImgContext", ex.get_Message());
				result = null;
			}
			return result;
		}
		public bool updateDownloadImgContext()
		{
			if (this.mImgInfo == null)
			{
				return false;
			}
			if (this.mStatus == 5)
			{
				StorageMgr.msgImg.delByTransID(this.mImgInfo.nMsgTransID);
				ChatMsg bySvrID = StorageMgr.chatMsg.getBySvrID(ChatMsgHelper.getTalker(this.mImgInfo.strToUserName, this.mImgInfo.strFromUserName), this.mImgInfo.nMsgSvrID);
				if (bySvrID != null)
				{
					bySvrID.strPath = this.mImgInfo.strImagePath;
				}
				StorageMgr.chatMsg.modifyMsg(bySvrID);
			}
			else
			{
				this.mImgInfo.nStatus = this.mStatus;
				this.mImgInfo.nLastModifyTime = (uint)(Util.getNowMilliseconds() / 1000.0);
				StorageMgr.msgImg.update(this.mImgInfo);
			}
			return true;
		}
		public void loadDownloadImgContext()
		{
			MsgTrans unCompleteDownloadingMsg = StorageMgr.msgImg.getUnCompleteDownloadingMsg();
			if (unCompleteDownloadingMsg != null)
			{
				this.mImgInfo = unCompleteDownloadingMsg;
				this.fileStream = this.getImgFileStream();
			}
		}
	}
}
